using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Xml;

using ClientGUI;

namespace ClientGUI.Main.PopUpWindows
{
    public partial class Preferences : Form
    {
        Configuration my_config;
        TextBox my_edt_box;
        ListViewItem my_selected_item = null;
        int my_selected_indice = 0;

        ModuleConfig my_selected_module_config = null;

        /// <summary>
        /// Prefrences constructor
        /// </summary>
        /// <param name="config"></param>
        public Preferences(Configuration config)
        {
            InitializeComponent();
            my_config = config;
            my_edt_box = new TextBox();
            config_lst.Controls.Add(my_edt_box);
            my_edt_box.Hide();
            my_edt_box.BorderStyle = BorderStyle.None;
            my_edt_box.KeyPress += new KeyPressEventHandler(my_edt_box_KeyPress);

            config_lst.Click += new EventHandler(config_lst_Click);

            for (int i = 0; i < config.Length; ++i)
            {
                ModuleConfig conf = config[i];
                modules_lst.Items.Add(conf.Name);
            }
        }

        void config_lst_Click(object sender, EventArgs e)
        {
            my_edt_box.Focus();
        }

        void my_edt_box_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) //return
            {
                my_selected_item.SubItems[1].Text = my_edt_box.Text;
                my_edt_box.Hide();

                KeyValuePair<String, String> pair = new KeyValuePair<string, string>(
                    my_selected_item.SubItems[0].Text,
                    my_edt_box.Text);
                my_selected_module_config[my_selected_indice] = pair;
            }
            if (e.KeyChar == 37) //escape?
            {
                my_edt_box.Hide();
            }
        }

        private void modules_lst_SelectedIndexChanged(object sender, EventArgs e)
        {
            config_lst.Items.Clear();
            if (modules_lst.SelectedItems.Count == 1)
            {
                String name = modules_lst.SelectedItems[0].Text;
                ModuleConfig conf = my_config.GetModuleConfig(name);
                for (int i = 0; i < conf.Length; ++i)
                {
                    String[] columns = new String[2];
                    columns[0] = conf[i].Key;
                    columns[1] = conf[i].Value;
                    config_lst.Items.Add(new ListViewItem(columns));
                }
                my_selected_module_config = conf;
            }

        }

        private void config_lst_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (config_lst.SelectedItems.Count == 1)
            {
                int x = config_lst.SelectedItems[0].SubItems[1].Bounds.X;
                int y = config_lst.SelectedItems[0].SubItems[1].Bounds.Y;
                int w = config_lst.SelectedItems[0].SubItems[1].Bounds.Width;
                int h = config_lst.SelectedItems[0].SubItems[1].Bounds.Height;



                my_edt_box.Left = x + 3;
                my_edt_box.Top = y;
                my_edt_box.Width = w - 3;
                my_edt_box.Height = h;
                my_edt_box.Font = config_lst.Font;
                my_edt_box.Show();
                my_edt_box.Text = config_lst.SelectedItems[0].SubItems[1].Text;

                my_edt_box.Focus();
                my_edt_box.SelectAll();

                my_selected_item = config_lst.SelectedItems[0];
                my_selected_indice = config_lst.SelectedIndices[0];
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Dispose();
        }
    }
}